;;; init-base.el --- Basical settings -*- lexical-binding: t -*-
;;; Commentary:

;;; Code:

;; emacs 异步支持
(use-package async :defer 2 :ensure t)

;; posframe 支持
(use-package posframe :defer 2 :ensure t)

;; Inspired by: emacs-china.org/t/posframe/20327?u=harl
(defun +posframe--image-preview-in-posframe (image-path &optional max-width max-height tmp-buf)
  "Display the image at IMAGE-PATH in a posframe."
  (let* ((frame-size (list (frame-pixel-width) (frame-pixel-height)))
         (image (create-image image-path 'png nil))
         (size (image-size image t))
         (scaled-dimensions
          (let* ((max-width (or max-width (* 0.4 (car frame-size))))
                 (max-height (or max-height (* 0.25 (cadr frame-size))))
                 (scale (min (/ max-width (car size))
                             (/ max-height (cdr size)))))
            (cons (truncate (* (car size) scale))
                  (truncate (* (cdr size) scale))))))
    (if (file-exists-p image-path)
        (let ((image-buffer (get-buffer-create (or tmp-buf " *tmp-buf*"))))
          (with-current-buffer image-buffer
            (unless (eq (point-max) 0) (erase-buffer))
            (insert-image (create-image image-path 'png nil :width (car scaled-dimensions)))
            (image-mode))
          (when (posframe-workable-p)
            (posframe-show image-buffer
                           :position (cons 0 0)
                           :poshandler #'posframe-poshandler-frame-center
                           :posframe-width (car scaled-dimensions)
                           :posframe-height (cdr scaled-dimensions)
                           :border-width 4))
          image-buffer)  ;; Return the buffer object
      (message "Image file does not exist: %s" image-path)
      nil)))

(use-package projectile
  :ensure t
  :hook (elpaca-after-init . projectile-mode)
  :bind (:map projectile-mode-map
              ([remap projectile-find-file]      . consult-projectile-find-file)
              ([remap projectile-find-dir]       . consult-projectile-find-dir)
              ([remap projectile-switch-project] . consult-projectile-switch-project))
  :init
  (setq projectile-keymap-prefix (kbd "C-c p")
        projectile-switch-project-action #'projectile-commander))

;; 记录最近打开的文件
(use-package recentf
  :ensure nil
  :hook (elpaca-after-init . recentf-mode)
  :custom
  (recentf-max-saved-items 25)
  (recentf-max-menu-items 25)
  :config
  (dolist (dir `(,(recentf-expand-file-name no-littering-var-directory)
                 ,(recentf-expand-file-name no-littering-etc-directory)
                 ,(recentf-expand-file-name (concat user-emacs-directory "straight"))
                 ,(recentf-expand-file-name (concat user-emacs-directory "elpaca"))
                 "^/usr/include/"))
    (add-to-list 'recentf-exclude dir)))

;; 保存 mini-buffer 历史
(use-package savehist
  :ensure nil
  :hook (elpaca-after-init . savehist-mode)
  :custom
  (savehist-additional-variables '(kill-ring
                                   search-ring
                                   compile-command
                                   buffer-name-history))
  (history-delete-duplicates t) ; 删除重复条目
  (savehist-save-minibuffer-history t) ; 保存 minibuffer 中的历史
  (history-length 500))

(provide 'init-base)
;;; init-base.el ends here
